export default {
	data() {
		return {
			//数据
      allMenu: [], // all菜单
      operType: this.get("operType"),
      roleName: this.get("roleName"),
			menuOne: [], // 菜单
			menuTwo: [], // 菜单
			menuThree: [], // 菜单
			menuFour: [], // 菜单
			menuFive: [], // 菜单
			menuSix: [], // 菜单
      menuSeven: [], // 菜单
			roleId: this.get("addRoleid"),
			deleteNewsparameters: { //添加菜单id集合
				data: {
					roleName: null,
					applicationList: []
				}
			},
			menuList: [],
			fatherIndex: 0, //一级菜单index
		}
	},
	updated:function(){
		if(this.getParamValue("fatherIndex")){
			this.fatherIndex=this.getParamValue("fatherIndex");
		}
	},
	created() { //调用方法
		let that = this;
		that.findAllMenu();
		if(that.roleId != null){
			that.checkApp();
		}
		
	},
	mounted() {},
	methods: { //写函数方法

		// 查询所有菜单
		findAllMenu: function() {
			let that = this;
			that.$post(
					that.urls.findAllMenu
				)
				.then(function(res) {
					if(res.success) {
						that.menuOne = res.data[0];
						that.menuTwo = res.data[1];
						that.menuThree = res.data[2];
						that.menuFour = res.data[3];
						that.menuFive = res.data[4];
						that.menuSix = res.data[5];
						that.menuSeven = res.data[6];
						let menu = res.data;
			            if(menu != null){
			              let roleId = [];
			              menu.forEach(function (e) {
			                if(e != null){
			                  e.forEach(function (o) {
			                    roleId.push(o.id);
			                  })
			                }
			              })
			              that.allMenu = roleId;
			              var timer=setTimeout(function(){
			              	var num=0;
			              	for(var i=1;i<=menu.length;i++){
			              		num+=$(".Role-box ul:nth-child("+(i)+")").width();
			              	}
			              	$(".Role-box").width(num+30);
			              	clearTimeout(timer);
			              },1000)
			            }


					}
				})

		},
		//编辑时查询该角色所有已有权限的菜单
		checkApp: function() {
			let that = this

			that.$post(
					that.urls.getRoleMenu, {
						id: that.roleId
					}
				)
				.then(function(res) {
//					console.log("查询所有已有权限的菜单", res)
					if(res.success) {
						let menu = res.data

            if(menu != null){
              let roleId = [];
              menu.forEach(function (e) {

                if(e != null){
                  e.forEach(function (o) {
                    roleId.push(o.id);
                  })
                }
              })
              that.deleteNewsparameters.data.applicationList = roleId;
            }

					} else {
						that.menuList = null
						layer.alert(res.errorMessage);
					}
				})
				.catch(function(err) {
					if(err.response) {
						console.log(err.response)
						//控制台打印错误返回的内容
						layer.msg('查询失败！', {
							time: 3000, //3s后自动关闭
						});
					}
				})
		},
		// 添加或修改角色
		addRole: function() {
			let that = this;
			let rolePrivs = that.deleteNewsparameters.data.applicationList.length; // 权限数量，菜单总和数
			console.log("菜单长度",rolePrivs)
      let name = $('#roleName').val();

			if(!name){
        layer.msg('角色名称不能为空!', {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      if(name.length > 10){
        layer.msg('角色名称不能超过10个字符!', {
          time: 3000, //3s后自动关闭
        });
        return;
      }

      that.$post(
        that.urls.findRole,
        {"roleName": name, "id": that.roleId}
      )
        .then(function(res) {
          if(res.success) {

            if(res.data != null){
              layer.msg('角色名称重复,请重新输入!', {
                time: 3000, //3s后自动关闭
              });
              return;
            }

            if(that.roleId == null) {// 添加
              that.$post(
                that.urls.addRole, {
                  roleName: name,
                  rolePrivs: rolePrivs
                }
              )
                .then(function(res) {
                  if(res.success) {
                    let addroleId = res.data; // 返回的角色id
                    that.roleAddMenu(addroleId);
                    console.log("addroleId",addroleId)
                  }
                })

            } else {//修改
              that.$post(
                that.urls.editRole, {
                  id: that.roleId,
                  roleName: name,
                  rolePrivs: rolePrivs
                } // menuId 为菜单集合id
              )
                .then(function(res) {
                  if(res.success) {
                    console.log("修改res",res)
                    that.editMenuByRole();
                  }
                })

            }

          }
        })


		},
		//修改该角色菜单权限
		editMenuByRole: function() {
			let that = this;
			console.log("修改该角色菜单权限id",that.deleteNewsparameters.data.applicationList)
			that.$post(
					that.urls.editMenuByRole, {
						roleId: that.roleId,
						menuId: that.deleteNewsparameters.data.applicationList,
					}
				)
				.then(function(res) {
					if(res.success) {
						console.log("修改该角色菜单权限res",res)
            layer.msg('成功！', {
              time: 3000, //3s后自动关闭
            });

            that.$router.push({
              path: "roleList?fatherIndex="+this.fatherIndex,
            })
					}
				})
				.catch(function(err) {
					if(err.response) {
						console.log(err.response)
						//控制台打印错误返回的内容
						layer.msg('查询失败！', {
							time: 3000, //3s后自动关闭
						});
					}
				})
		},
		//给该角色添加菜单权限
		roleAddMenu: function(roleId) {
			let that = this;
			console.log("给新角色添加菜单权限id",that.deleteNewsparameters.data.applicationList)
			that.$post(
					that.urls.roleAddMenu, {
						roleId: roleId,
						menuId: that.deleteNewsparameters.data.applicationList,
					}
				)
				.then(function(res) {
					if(res.success) {
						console.log("给新角色添加菜单权限res",res)
            layer.msg('成功！', {
              time: 3000, //3s后自动关闭
            });

            that.$router.push({
              path: "roleList?fatherIndex="+this.fatherIndex,
            })

					}
				})
				.catch(function(err) {
					if(err.response) {
						console.log(err.response)
						//控制台打印错误返回的内容
						layer.msg('查询失败！', {
							time: 3000, //3s后自动关闭
						});
					}
				})
		},
     // 全选
    checkedAll: function() {
      let that = this;
      if ($('#checkedAll').is(':checked') == true) {
        that.deleteNewsparameters.data.applicationList = that.allMenu;
        $('#checkOne').prop('checked', true);
        $('#checkTwo').prop('checked', true);
        $('#checkThree').prop('checked', true);
        $('#checkFour').prop('checked', true);
        $('#checkFive').prop('checked', true);
        $('#checkSix').prop('checked', true);
        $('#checkSeven').prop('checked', true);

      } else {
        that.deleteNewsparameters.data.applicationList = [];
        $('#checkOne').prop('checked', false);
        $('#checkTwo').prop('checked', false);
        $('#checkThree').prop('checked', false);
        $('#checkFour').prop('checked', false);
        $('#checkFive').prop('checked', false);
        $('#checkSix').prop('checked', false);
        $('#checkSeven').prop('checked', false);
      }
    },
    // 门户全选
    checkOne: function () {
      let inputs = $("input[name='checkOnes']")
      if($('#checkOne').is(':checked') == true ){
        inputs.each(function() {
          if($(this).is(':checked') == false ){
            $(this).click();
          }
        })
      }  else {
        inputs.each(function() {
          if($(this).is(':checked') == true ){
            $(this).click();
          }
       //   $(this).prop('checked', false);
        })
      }
    },
    // 基地信息化全选
    checkTwo: function () {
      let inputs = $("input[name='checkTwos']")
      if($('#checkTwo').is(':checked') == true ){
        inputs.each(function() {
          if($(this).is(':checked') == false ){
            $(this).click();
          }
        })
      }  else {
        inputs.each(function() {
          if($(this).is(':checked') == true ){
            $(this).click();
          }
        })
      }
    },
   // 项目信息化
    checkThree: function () {
      let inputs = $("input[name='checkThrees']")
      if($('#checkThree').is(':checked') == true ){
        inputs.each(function() {
          if($(this).is(':checked') == false ){
            $(this).click();
          }
        })
      }  else {
        inputs.each(function() {
          if($(this).is(':checked') == true ){
            $(this).click();
          }
        })
      }
    },
    // 教育信息化
    checkFour: function () {
      let inputs = $("input[name='checkFours']")
      if($('#checkFour').is(':checked') == true ){
        inputs.each(function() {
          if($(this).is(':checked') == false ){
            $(this).click();
          }
        })
      }  else {
        inputs.each(function() {
          if($(this).is(':checked') == true ){
            $(this).click();
          }
        })
      }
    },
    // 数据资源库
    checkFive: function () {
      let inputs = $("input[name='checkFives']")
      if($('#checkFive').is(':checked') == true ){
        inputs.each(function() {
          if($(this).is(':checked') == false ){
            $(this).click();
          }
        })
      }  else {
        inputs.each(function() {
          if($(this).is(':checked') == true ){
            $(this).click();
          }
        })
      }
    },
    // 后台配置
    checkSix: function () {
      let inputs = $("input[name='checkSixs']")
      if($('#checkSix').is(':checked') == true ){
        inputs.each(function() {
          if($(this).is(':checked') == false ){
            $(this).click();
          }
        })
      }  else {
        inputs.each(function() {
          if($(this).is(':checked') == true ){
            $(this).click();
          }
        })
      }
    },
    // 小程序
    checkSeven: function () {
      let inputs = $("input[name='checkSevens']")
      if($('#checkSeven').is(':checked') == true ){
        inputs.each(function() {
          if($(this).is(':checked') == false ){
            $(this).click();
          }
        })
      }  else {
        inputs.each(function() {
          if($(this).is(':checked') == true ){
            $(this).click();
          }
        })
      }
    },

	}
}
